package com.doctor.ui.wxapi;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.text.Html;

import com.doctor.bean.DiseaseRecordBean;
import com.doctor.bean.PatientFileBean;
import com.doctor.comm.LoadingTip;
import com.doctor.comm.MyApplication;
import com.doctor.comm.TextUtil;
import com.doctor.net.NetUtil;
import com.doctor.ui.R;
import com.tencent.mm.sdk.modelmsg.SendMessageToWX;
import com.tencent.mm.sdk.modelmsg.WXImageObject;
import com.tencent.mm.sdk.modelmsg.WXMediaMessage;
import com.tencent.mm.sdk.modelmsg.WXTextObject;

/**
 * 微信分享
 * Created on 2016/11/15
 */
public class WXShare {
	
	
	private WXShare() {
		
	}
	
	
	/**
	 * 微信分享文本
	 * @param text 要分享的文本内容
	 */
	public static void shareText(String text) {
		if (text == null || text.length() <= 0) {
			return;
		}
		if (!NetUtil.hasNetwork(MyApplication.getInstance())) {
			LoadingTip.showToast(MyApplication.getInstance(), MyApplication.getInstance().getString(R.string.net_work_error));
			return;
		}
		
		WXTextObject textObj = new WXTextObject();
		textObj.text = text;
		WXMediaMessage msg = new WXMediaMessage();
		msg.mediaObject = textObj;
		if (text.length() > 30)
			msg.description = text.substring(0, 30);  //描述文字最多30个字
		else
			msg.description = text;
		
		SendMessageToWX.Req req = new SendMessageToWX.Req();
		//分享至好友会话
		req.scene = SendMessageToWX.Req.WXSceneSession;
		req.transaction = String.valueOf(System.currentTimeMillis());
		req.message = msg;
		//调用api接口发送数据到微信
		MyWXApi.getWXApi().sendReq(req);
	}
	
	
	/**
	 * 微信分享患者档案信息及病历资料
	 * @param patientFile 患者档案
	 * @param diseaseRecord 病历资料
	 */
	public static void sharePatientDiseaseRecord(PatientFileBean patientFile, DiseaseRecordBean diseaseRecord) {
		if (patientFile == null || diseaseRecord == null) {
			return;
		}
		if (!NetUtil.hasNetwork(MyApplication.getInstance())) {
			LoadingTip.showToast(MyApplication.getInstance(), MyApplication.getInstance().getString(R.string.net_work_error));
			return;
		}
		
		StringBuilder sb = new StringBuilder();
		sb.append("档案信息");
		sb.append("\r\n姓名：");
		sb.append(patientFile.getName());
		if (!TextUtil.isEmpty(patientFile.getSex())) {
			sb.append("\r\n性别：");
			sb.append(patientFile.getSex());
		}
		if (!TextUtil.isEmpty(patientFile.getMarriage())) {
			sb.append("\r\n婚否：");
			sb.append(patientFile.getMarriage());
		}
		if (!TextUtil.isEmpty(patientFile.getBirthDay())) {
			sb.append("\r\n出生日期：");
			sb.append(patientFile.getBirthDay());
		}
		if (!TextUtil.isEmpty(patientFile.getBody())) {
			sb.append("\r\n体质：");
			sb.append(patientFile.getBody());
		}
		if (!TextUtil.isEmpty(patientFile.getBloodType())) {
			sb.append("\r\n血型：");
			sb.append(patientFile.getBloodType());
		}
		if (!TextUtil.isEmpty(patientFile.getContacts())) {
			sb.append("\r\n联系人：");
			sb.append(patientFile.getContacts());
		}
		if (!TextUtil.isEmpty(patientFile.getContactsMobile())) {
			sb.append("\r\n联系人电话：");
			sb.append(patientFile.getContactsMobile());
		}
		if (!TextUtil.isEmpty(patientFile.getAllergy())) {
			sb.append("\r\n药物过敏史：");
			sb.append(patientFile.getAllergy());
		}
		if (!TextUtil.isEmpty(patientFile.getFamilyHistory())) {
			sb.append("\r\n家族史：");
			sb.append(patientFile.getFamilyHistory());
		}
		if (!TextUtil.isEmpty(patientFile.getPersonalHistory())) {
			sb.append("\r\n个人史：");
			sb.append(patientFile.getPersonalHistory());
		}
		if (!TextUtil.isEmpty(patientFile.getDiseaseHistory())) {
			sb.append("\r\n疾病史：");
			sb.append(patientFile.getDiseaseHistory());
		}
		
		sb.append("\r\n\r\n病历资料");
		if (!TextUtil.isEmpty(diseaseRecord.getTime())) {
			sb.append("\r\n就诊时间：");
			sb.append(diseaseRecord.getTime());
		}
		if (!TextUtil.isEmpty(diseaseRecord.getComplain())) {
			sb.append("\r\n主诉：");
			sb.append(diseaseRecord.getComplain());
		}
		if (!TextUtil.isEmpty(diseaseRecord.getPresent())) {
			sb.append("\r\n现病史：");
			sb.append(diseaseRecord.getPresent());
		}
		if (!TextUtil.isEmpty(diseaseRecord.getPhysicalCheck())) {
			sb.append("\r\n体格检查：");
			sb.append(diseaseRecord.getPhysicalCheck());
		}
		if (!TextUtil.isEmpty(diseaseRecord.getAuxiliaryCheck())) {
			sb.append("\r\n辅助检查：");
			sb.append(diseaseRecord.getAuxiliaryCheck());
		}
		if (!TextUtil.isEmpty(diseaseRecord.getInitialDiagnosis())) {
			sb.append("\r\n初步诊断：");
			sb.append(diseaseRecord.getInitialDiagnosis());
		}
		if (!TextUtil.isEmpty(diseaseRecord.getPrescription())) {
			sb.append("\r\n处方：");
			sb.append(diseaseRecord.getPrescription());
		}
		if (!TextUtil.isEmpty(diseaseRecord.getDoctorAdvice())) {
			sb.append("\r\n医嘱：");
			sb.append(diseaseRecord.getDoctorAdvice());
		}
		if (!TextUtil.isEmpty(diseaseRecord.getHealthAna())) {
			sb.append("\r\n健康分析：");
			sb.append(Html.fromHtml(diseaseRecord.getHealthAna()).toString());
		}
		if (!TextUtil.isEmpty(diseaseRecord.getHealthInstruct())) {
			sb.append("\r\n健康指导：");
			sb.append(Html.fromHtml(diseaseRecord.getHealthInstruct()).toString());
		}
		if (!TextUtil.isEmpty(diseaseRecord.getHealthDirect())) {
			sb.append("\r\n健康提示：");
			sb.append(Html.fromHtml(diseaseRecord.getHealthDirect()).toString());
		}
		
		shareText(sb.toString());
	}
	
	
	/**
	 * 微信分享图片
	 * @param picPath 图片路径
	 */
	public static void sharePic(String picPath) {
		if (TextUtil.isEmpty(picPath)) {
			return;
		}
		if (!NetUtil.hasNetwork(MyApplication.getInstance())) {
			LoadingTip.showToast(MyApplication.getInstance(), MyApplication.getInstance().getString(R.string.net_work_error));
			return;
		}
		
		Bitmap bitmap = BitmapFactory.decodeFile(picPath);
		if (bitmap == null) {
			return;
		}
		
		WXImageObject imgObj = new WXImageObject(bitmap);
//		imgObj.setImagePath(picPath);
		WXMediaMessage msg = new WXMediaMessage();
		msg.mediaObject = imgObj;
		
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		Bitmap thumbBmp = Bitmap.createScaledBitmap(bitmap, 80, 80, true);
		bitmap.recycle();
		thumbBmp.compress(CompressFormat.PNG, 100, bos);
		//设置缩略图
		msg.thumbData = bos.toByteArray();
		try {
			bos.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		thumbBmp.recycle();
		SendMessageToWX.Req req = new SendMessageToWX.Req();
		req.transaction = String.valueOf(System.currentTimeMillis());
		req.message = msg;
		req.scene = SendMessageToWX.Req.WXSceneSession;
		MyWXApi.getWXApi().sendReq(req);
	}

}
